﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands
{
    public class SheepEarBuglesProgenyQueryHandler : IRequestHandler<SheepEarBuglesProgenyQueryCommand, Result>
    {
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;

        public SheepEarBuglesProgenyQueryHandler(IBaseRepository<SheepArchives> sheepArchivesRepository)
        {
            _sheepArchivesRepository = sheepArchivesRepository;
        }
        /// <summary>
        /// 查询后裔耳号后裔
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepEarBuglesProgenyQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var sheepArchives = await(from a in await _sheepArchivesRepository.GetAll()
                                          where a.RecordType == 2//后裔
                                          select new SheepArchives()
                                          {
                                              RecordId = a.RecordId,
                                              RecordAuricle = a.RecordAuricle,
                                          }).ToListAsync();
                result.Data = sheepArchives;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
